﻿using System;
using System.Windows.Media;
using DependencyAnalysis.DataModel;
using theeg.GraphWpf.Arrangement;
using theeg.GraphWpf.Models;

namespace theeg.DependencyAnalysis.CommonAddin.Models
{
    public static class GraphBuilder
    {
        public static readonly Random Random = new Random();

        public static T BuildNode<T>(IDescriptor descriptor) where T : Node, new()
        {
            var node = new T
            {
                Width = 250,
                Height = 54,
                Text = descriptor.FullName,
                NodeShape = NodeShape.RoundRect,
                DataObject = descriptor,
                Left = Random.NextDouble() * 100 + 1,
                Top = Random.NextDouble() * 100 + 1
            };
            ColorNode(node, descriptor);
            return node;
        }

        private static void ColorNode(Node node, IDescriptor descriptor)
        {
            var subGraph = node as SubGraph;
            if (subGraph != null)
            {
                subGraph.Fill = new SolidColorBrush(new Color { A = 255, R = 30, G = 30, B = 30 });
            }
            node.Stroke = new SolidColorBrush(new Color { A = 255, R = 129, G = 129, B = 129 });
            node.Foreground = new SolidColorBrush(Colors.White);
            node.Highlight = new SolidColorBrush(Colors.Gold);

            var module = descriptor as ModuleDescriptor;
            if (module != null)
            {
                if (module.IsPartOfFramework)
                {
                    node.Background = new SolidColorBrush(new Color { A = 255, R = 66, G = 66, B = 66 });
                }
                else if (module.IsPartOfProject)
                {
                    node.Background = new SolidColorBrush(new Color { A = 255, R = 9, G = 65, B = 103 });
                }
                else
                {
                    node.Background = new SolidColorBrush(new Color { A = 255, R = 255, G = 165, B = 63 });
                }
                return;
            }
            if (descriptor is SubmoduleDescriptor)
            {
                node.Background = new SolidColorBrush(new Color { A = 255, R = 14, G = 97, B = 154 });
            }
            if (descriptor is TypeDescriptor)
            {
                node.Background = new SolidColorBrush(new Color { A = 255, R = 19, G = 130, B = 206 });
            }
            if (descriptor is MemberDescriptor)
            {
                node.Background = new SolidColorBrush(new Color { A = 255, R = 224, G = 224, B = 224 });
                node.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        public static void ApplyCompactLayout(Node node)
        {
            node.Height = 27;
            node.Width = 500;
            node.NodeShape = NodeShape.Rectangle;
        }

        public static Transition BuildTransition(Node source, Node target)
        {
            var transition = new Transition
            {
                SourceCapital = TransitionCapital.Plain,
                TargetCapital = TransitionCapital.Arrow,
                TransitionStyle = TransitionStyle.Bezier,
                RoutingStyle = RoutingStyle.FreeHandHorizontal,
                Brush = new SolidColorBrush(new Color { A = 255, R = 192, G = 192, B = 192 }),
                Magnitude = 2
            };
            transition.SetConnection(source, target);
            return transition;
        }
    }
}
